﻿Namespace B1Data
    ''' <summary>
    ''' 字段赋值规则
    ''' 设置字段的编码规则赋值
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class CodeRule
        Implements ICodeRule

        Protected _ForePart As String = ""
        Protected _BackPart As String = ""
        Protected _CurrentCode As String
        Protected _FieldName As String
        Protected _Table As SAPBoAddon.B1Data.B1DataTable
        Protected _FatherCode As String
        Protected _AutoRecordCode As Boolean = True
        Protected MaskId As String = "0000"

        Sub New(ByVal Column As B1Data.B1DataColumn)
            _Table = Column.Table
            _FieldName = Column.ColumnName
        End Sub

        Sub New(ByVal table As SAPBoAddon.B1Data.B1DataTable, ByVal FieldName As String)
            _Table = table
            _FieldName = FieldName
        End Sub

        ''' <summary>
        ''' 执行NextCode方法时自动记录CurrentCode
        ''' 需在NextCode中指定
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AutoRecordCode As Boolean
            Set(ByVal value As Boolean)
                _AutoRecordCode = True
            End Set
            Get
                Return _AutoRecordCode
            End Get
        End Property

        ''' <summary>
        ''' 当前编码，下次编码的基础
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CurrentCode As String Implements SAPBoAddon.B1Data.ICodeRule.CurrentCode
            Get
                Return _CurrentCode
            End Get
        End Property

        Public ReadOnly Property FieldName As String Implements SAPBoAddon.B1Data.ICodeRule.FieldName
            Get
                Return _FieldName
            End Get
        End Property

        ''' <summary>
        ''' 格式化字符
        ''' </summary>
        ''' <param name="Value">字符</param>
        ''' <param name="MaskId">格式</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Function FormatString(ByVal Value As String, ByVal MaskId As String) As String
            If Value.Length < MaskId.Length Then
                Return MaskId.Substring(0, MaskId.Length - Value.Length) & Value.ToString
            Else
                Return Value
            End If
        End Function

        ''' <summary>
        ''' 去除当前编码的前缀
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Function getCurrentCodeNoForcPart() As String
            If String.IsNullOrEmpty(CurrentCode) Then
                Return ""
            Else
                If Not String.IsNullOrEmpty(_ForePart) Then
                    If CurrentCode Like _ForePart & "*" Then
                        Return CurrentCode.Substring(_ForePart.Length + 1, CurrentCode.Length - _ForePart.Length - 1)
                    Else
                        Return ""
                    End If
                Else
                    Return CurrentCode
                End If
            End If
        End Function

        ''' <summary>
        ''' 去除当前编码的后缀
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Function getCurrentCodeNoBackPart() As String
            If String.IsNullOrEmpty(CurrentCode) Then
                Return ""
            Else
                If CurrentCode Like "*" & _BackPart Then
                    Return CurrentCode.Substring(0, CurrentCode.Length - _BackPart.Length)
                Else
                    Return ""
                End If
            End If
        End Function

        ''' <summary>
        ''' 去除编码的前缀和后缀
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Function getCurrentCodeNoBothPart() As String
            Dim tmpStr As String
            If String.IsNullOrEmpty(CurrentCode) Then
                Return ""
            Else
                If CurrentCode Like _ForePart & "*" & _BackPart Then
                    tmpStr = (CurrentCode.Substring(0, CurrentCode.Length - _BackPart.Length))
                    Return tmpStr.Substring(_ForePart.Length - 1, CurrentCode.Length - _ForePart.Length)
                Else
                    Return ""
                End If
            End If
        End Function

        ''' <summary>
        ''' 获取下个编码
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public MustOverride Function NextCode() As String Implements SAPBoAddon.B1Data.ICodeRule.NextCode

        ''' <summary>
        ''' 获取下个编码（带参数）
        ''' </summary>
        ''' <param name="params"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public MustOverride Function NextCode(ByVal ParamArray params() As Object) As String Implements SAPBoAddon.B1Data.ICodeRule.NextCode

        ''' <summary>
        ''' 刷新当前编码
        ''' </summary>
        ''' <remarks></remarks>
        Public MustOverride Sub RefreshCurrenCode() Implements SAPBoAddon.B1Data.ICodeRule.RefreshCurrenCode

        ''' <summary>
        ''' 刷新当前编码
        ''' </summary>
        ''' <param name="Key"></param>
        ''' <remarks></remarks>
        Public MustOverride Sub RefreshCurrenCode(ByVal Key As Object) Implements SAPBoAddon.B1Data.ICodeRule.RefreshCurrenCode

        'Public MustOverride Sub SetParam(ByVal ParamArray Items() As Object) Implements SAPBoAddon.B1Data.ICodeRule.SetParam

        ''' <summary>
        ''' 编码后缀
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property BackPart As String Implements SAPBoAddon.B1Data.ICodeRule.BackPart
            Get
                Return _BackPart
            End Get
            Set(ByVal value As String)
                _BackPart = value
            End Set
        End Property

        ''' <summary>
        ''' 编码前缀
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ForePart As String Implements SAPBoAddon.B1Data.ICodeRule.ForePart
            Get
                Return _ForePart
            End Get
            Set(ByVal value As String)
                _ForePart = value
            End Set
        End Property

    End Class
End Namespace

